package com.tianyi.role;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.tianyi.dao.entity.role.Role;
import com.tianyi.dao.entity.role.RoleExample;
import com.tianyi.dao.entity.roleMenuRecord.RoleMenuRecord;
import com.tianyi.dao.entity.roleMenuRecord.RoleMenuRecordExample;
import com.tianyi.dao.mapper.CommMapper;
import com.tianyi.dao.mapper.RoleMapper;
import com.tianyi.dao.mapper.RoleMenuRecordMapper;
import com.tianyi.lib.req.RoleReq;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 权限base service
 *
 * @author cuixuhui
 * @date 2020/06/24
 */
@Service
public class RoleBaseService {

    private static final Logger logger = LoggerFactory.getLogger(RoleBaseService.class);

    @Autowired
    private RoleService roleService;

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RoleMenuRecordMapper roleMenuRecordMapper;
    @Autowired
    private CommMapper commMapper;

    //================================Role============================================

    public List<Role> queryExistRoleByReq(RoleReq req) {
        RoleExample example = new RoleExample();
        example.or().andIdEqualTo(req.getId());
        example.or().andRoleNameEqualTo(req.getRoleName());
        example.or().andRoleCodeEqualTo(req.getRoleCode());

        List<Role> roles = roleMapper.selectByExample(example);
        return roles;
    }

    public Role queryRoleByRoleId(Long roleId) {

        if (Objects.isNull(roleId)){
            return null;
        }
        return roleMapper.selectByPrimaryKey(roleId);
    }

    public List<Role> queryAllRolePullDown() {
        RoleExample example = new RoleExample();
        List<Role> roles = roleMapper.selectByExample(example);
        return roles;
    }

    public Role insertRole(Role role) {
        if (Objects.isNull(role)){
            return null;
        }
        roleMapper.insert(role);
        return role;
    }

    public Role updateRole(Role role) {
        if (Objects.isNull(role)){
            return null;
        }
        roleMapper.updateByPrimaryKey(role);
        return role;
    }

    public PageInfo<RoleMenuIds> queryRolePage(RoleReq req) {

        RoleExample example = new RoleExample();
        example.setOrderByClause("id ASC");
        RoleExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(req.getRoleName())){
            criteria.andRoleNameEqualTo(req.getRoleName());
        }
        if (StringUtils.isNotBlank(req.getRoleCode())){
            criteria.andRoleCodeEqualTo(req.getRoleCode());
        }
        if (Objects.nonNull(req.getRoleLevel())){
            criteria.andRoleLevelEqualTo(req.getRoleLevel());
        }
        Page<Role> rolePages = roleMapper.selectByExample(example, new RowBounds(req.getPageNo(), req.getPageSize()));
        PageInfo<Role> rolePageInfos = rolePages.toPageInfo();
        PageInfo<RoleMenuIds> rolePagesMenuIds = new PageInfo<>();
        BeanUtils.copyProperties(rolePages, rolePagesMenuIds);
        List<RoleMenuIds> roleMenuIdsList = new ArrayList<>();
        for(Role role : rolePages) {
            List<Long> menuIds = roleService.getMenuIdsByRoleId(role.getId());
            RoleMenuIds roleMenuIdsTemp = new RoleMenuIds();
            roleMenuIdsTemp.setMenuIds(menuIds);
            roleMenuIdsTemp.setRole(role);
            roleMenuIdsList.add(roleMenuIdsTemp);
        }
        rolePagesMenuIds.setList(roleMenuIdsList);
        return rolePagesMenuIds;
    }

    //================================RoleMenuRecord==================================

    public List<RoleMenuRecord> queryRoleMenusByRoleId(Long roleId) {

        if (Objects.isNull(roleId)){
            return null;
        }
        RoleMenuRecordExample example = new RoleMenuRecordExample();
        example.createCriteria().andRoleIdEqualTo(roleId);
        List<RoleMenuRecord> roleMenuRecords = roleMenuRecordMapper.selectByExample(example);
        return roleMenuRecords;
    }

    public List<RoleMenuRecord> batchInsertRoleMenus(List<RoleMenuRecord> roleMenuRecords) {

        if (CollectionUtils.isEmpty(roleMenuRecords)){
            return null;
        }
        int batchInsert = roleMenuRecordMapper.batchInsert(roleMenuRecords);
        return roleMenuRecords;
    }

    public void deleteRoleMenusByRoleId(Long roleId) {

        if (Objects.isNull(roleId)){
            return;
        }
        roleMenuRecordMapper.deleteByRoleId(roleId);
    }

    public void insertRoleMenus(List<RoleMenuRecord> roleMenuRecordsList) {

        if (Objects.isNull(roleMenuRecordsList)){
            return;
        }
        roleMenuRecordMapper.batchInsert(roleMenuRecordsList);
    }

}
